package cn.com.generaldata.os.util;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JTextArea;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.UnsupportedLookAndFeelException;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.awt.Font;

public class Flex2Html {

	private JFrame frame;
	private JTextArea htmlInput;
	private JTextArea flexInput;
	private JButton button;
	private JButton btnNewButton;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Flex2Html window = new Flex2Html();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public Flex2Html() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setResizable(false);
		frame.setFont(new Font("宋体", Font.PLAIN, 12));
		frame.setBounds(100, 100, 764, 650);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		try {
			javax.swing.UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
		} catch (ClassNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (InstantiationException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (IllegalAccessException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (UnsupportedLookAndFeelException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		flexInput = new JTextArea();
		flexInput.setLineWrap(true);
		
		htmlInput = new JTextArea();
		htmlInput.setLineWrap(true);
		
		button = new JButton("转换为编辑页面");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				doConvert();
			}
		});
		
		JLabel lblFlex = new JLabel("flex");
		
		JLabel lblHtml = new JLabel("html");
		
		btnNewButton = new JButton("转换为查看页面");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				doConvert2();
			}
		});
		GroupLayout groupLayout = new GroupLayout(frame.getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(37)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(lblHtml)
							.addGap(174)
							.addComponent(button, GroupLayout.PREFERRED_SIZE, 185, GroupLayout.PREFERRED_SIZE)
							.addGap(29)
							.addComponent(btnNewButton))
						.addComponent(htmlInput, GroupLayout.PREFERRED_SIZE, 682, GroupLayout.PREFERRED_SIZE)
						.addComponent(flexInput, GroupLayout.PREFERRED_SIZE, 678, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblFlex))
					.addContainerGap(39, Short.MAX_VALUE))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addComponent(lblFlex)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(flexInput, GroupLayout.PREFERRED_SIZE, 255, GroupLayout.PREFERRED_SIZE)
					.addGap(18)
					.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
						.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
							.addComponent(button)
							.addComponent(btnNewButton))
						.addComponent(lblHtml))
					.addPreferredGap(ComponentPlacement.RELATED, 21, Short.MAX_VALUE)
					.addComponent(htmlInput, GroupLayout.PREFERRED_SIZE, 245, GroupLayout.PREFERRED_SIZE)
					.addContainerGap())
		);
		frame.getContentPane().setLayout(groupLayout);
	}

	/**
	 * 转换为查看页面的html.
	 */
	protected void doConvert2() {
		String flex = flexInput.getText();
		if(flex.length()==0){
			return;
		}
		
		String regex="<s:Label text=\"(.*?)\".*?/>";
		Pattern p = Pattern.compile(regex,Pattern.DOTALL);
		Matcher m = p.matcher(flex);
		flex = m.replaceAll("$1");
		
		regex="<s:HGroup.*?>";
		p = Pattern.compile(regex,Pattern.DOTALL);
		m = p.matcher(flex);
		flex = m.replaceAll("");
		
		regex="<s:TextInput id=\"(.*?)\".*?/>";
		p = Pattern.compile(regex,Pattern.DOTALL);
		m = p.matcher(flex);
		flex = m.replaceAll("\\${obj.$1}<br/>");

		
		regex="<s:Button id=\"(.*?)\".*?label=\"(.*?)\".*?/>";
		p = Pattern.compile(regex,Pattern.DOTALL);
		m = p.matcher(flex);
		flex = m.replaceAll("<input type=\"button\" id=\"$1\" value=\"$2\"/><br/>");

		
		regex="<s:DropDownList id=\"(.*?)\".*?/>";
		p = Pattern.compile(regex,Pattern.DOTALL);
		m = p.matcher(flex);
		flex = m.replaceAll("\\${obj.$1}<br/>");
		
		regex="<s:TextArea id=\"(.*?)\".*?/>";
		p = Pattern.compile(regex,Pattern.DOTALL);
		m = p.matcher(flex);
		flex = m.replaceAll("\\${obj.$1}<br/>");

		//<mx:Spacer width="50%"/></s:backgroundFill>
		flex = flex.replaceAll("<mx:Spacer.*?/>", "");
		flex = flex.replaceAll("</?s:.*?>", "");
		
		flex = flex.replaceAll("\t+", "");
		flex = flex.replaceAll("\n+", "\n");
	
		htmlInput.setText(flex);
	}

	protected void doConvert() {
		String flex = flexInput.getText();
		if(flex.length()==0){
			return;
		}
		
		String regex="<s:Label text=\"(.*?)\".*?/>";
		Pattern p = Pattern.compile(regex,Pattern.DOTALL);
		Matcher m = p.matcher(flex);
		flex = m.replaceAll("$1");
		
		regex="<s:HGroup.*?>";
		p = Pattern.compile(regex,Pattern.DOTALL);
		m = p.matcher(flex);
		flex = m.replaceAll("");
		
		regex="<s:TextInput id=\"(.*?)\".*?/>";
		p = Pattern.compile(regex,Pattern.DOTALL);
		m = p.matcher(flex);
		flex = m.replaceAll("<input type=\"text\" name=\"$1\" id=\"$1\"/><br/>");

		
		regex="<s:Button id=\"(.*?)\".*?label=\"(.*?)\".*?/>";
		p = Pattern.compile(regex,Pattern.DOTALL);
		m = p.matcher(flex);
		flex = m.replaceAll("<input type=\"button\" id=\"$1\" value=\"$2\"/><br/>");

		
		regex="<s:DropDownList id=\"(.*?)\".*?/>";
		p = Pattern.compile(regex,Pattern.DOTALL);
		m = p.matcher(flex);
		flex = m.replaceAll("<select name=\"$1\" id=\"$1\"/><option value=''></option></select><br/>");
		
		regex="<s:TextArea id=\"(.*?)\".*?/>";
		p = Pattern.compile(regex,Pattern.DOTALL);
		m = p.matcher(flex);
		flex = m.replaceAll("<textarea name=\"$1\" id=\"$1\"/></textarea><br/>");

		//<mx:Spacer width="50%"/></s:backgroundFill>
		flex = flex.replaceAll("<mx:Spacer.*?/>", "");
		flex = flex.replaceAll("</?s:.*?>", "");
		
		flex = flex.replaceAll("\t+", "");
		flex = flex.replaceAll("\n+", "\n");
	
		htmlInput.setText(flex);
		
		//<s:HGroup width="50%"
		  //verticalAlign="middle">
		
		//<s:Label text="登录名："
		//		 width="90"
		//		 height="20"
		//		 textAlign="right"
		//		 verticalAlign="middle"/>
		
//		<s:TextInput id="txiuserLoginname"
//				 width="140"
//				 text=""
//				 textAlign="left" enabled="false"/>
		
//		<s:Button id="btnClose"
//		  label="关闭"
//		  click="{dispatchEvent(new CloseEvent(CloseEvent.CLOSE))}"/>
		
//		<s:DropDownList id="enterpriseUserUseSystem" 
//				width="140" 
//				dataProvider="{enterpriseUserUseSystemArray}" enabled="false">
//				</s:DropDownList>
		
		//<s:TextArea id="companySynopsis" width="475" enabled="false"/>
		
		
		
	}
}
